//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    goodsId: "",
    uploadType: "",
    addOrUpdate: "上传",
    // 
    imgUrl: baseJs.imgUrl,
    imgList: [],
    goodsName: "",
    goodsValue: "",
    goodsNum: "",
    goodsTime: "",
    goodsDes: "",
    storeType: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options);
    this.setData({
      uid: wx.getStorageSync('uid'),
      uploadType: options.uploadType,
      storeType: options.storeType,
      goodsId: options.goodsId,
    })
    if (this.data.uploadType == "add") {
      this.setData({
        addOrUpdate: "上传"
      })
    } else {
      this.setData({
        addOrUpdate: "保存修改"
      })
      this.AgentGetGoodsStoreInfo();
    }
  },

  // 图片预览
  imgPreview: function(e) {
    wx.previewImage({
      // 当前显示图片的http链接
      current: e.currentTarget.dataset.src,
      // 需要预览的图片http链接列表
      urls: [e.currentTarget.dataset.src]
    })
  },
  // chooseImg
  chooseImg: function(e) {
    var that = this;
    var base64 = this.data.base64;
    var imgSrc = this.data.imgSrc;
    var imgUrl = this.data.imgUrl;
    var imgList = this.data.imgList;
    wx.chooseImage({
      // 最多可以选择的图片张数，默认9
      count: 1,
      // 可以指定是原图还是压缩图，默认二者都有
      sizeType: ['original', 'compressed'],
      // 可以指定来源是相册还是相机，默认二者都有
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        wx.request({
          url: tempFilePaths,
          method: 'GET',
          responseType: 'arraybuffer',
          success: function(res) {
            base64 = wx.arrayBufferToBase64(res.data);
            that.setData({
              base64: base64,
              imgSrc: 'data:image/jpg;base64,' + base64
            })
            // console.log(base64)
            wx.request({
              url: baseJs.api + baseJs.encryUrl("method=LoadPic"),
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              method: 'post',
              data: baseJs.json2Form({
                HeadImage: base64,
                Type: 3
              }),
              success: function(res) {
                // console.log(res);
                imgList.push(res.data.ResultData);
                that.setData({
                  imgList: imgList,
                })
              }
            })
          }
        })
      }
    })
  },
  // delTap
  delTap: function(e) {
    var imgList = this.data.imgList;
    imgList.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      imgList: imgList,
    })
  },
  // 过期时间
  bindDateChange: function(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      goodsTime: e.detail.value
    })
  },
  // 产品备注
  describeTextarea: function (e) {
    this.setData({
      goodsDes: e.detail.value,
    })
  },
  // 要修改的商品信息
  AgentGetGoodsStoreInfo:function(){
    var that = this;
    var goodsId = this.data.goodsId;
    var goodsName = this.data.goodsName;
    var goodsValue = this.data.goodsValue;
    var goodsNum = this.data.goodsNum;
    var goodsTime = this.data.goodsTime;
    var imgList = this.data.imgList;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AgentGetGoodsStoreInfo&GoodsId=" + goodsId),
      success: function (res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data));
        var arr = json.ResultData.ImgUrl.split(';');
        for (var i = 0; i < arr.length; i++) {
          if (arr[i].length > 0) {
            imgList.push(arr[i]);
          }
        }
        if (json.ResultFlag == 1) {
         that.setData({
           goodsName: json.ResultData.GoodsName,
           goodsValue: json.ResultData.Price,
           goodsNum: json.ResultData.GoodsNum,
           goodsTime: json.ResultData.expireTime,
           goodsDes: json.ResultData.Describe,
           imgList: imgList
         })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })

  },
  // 上传
  formSubmit: function(e) {
    console.log(e);
    var that = this;
    var uid = this.data.uid;
    // var goodsName = this.data.goodsName;
    // var goodsValue = this.data.goodsValue;
    // var goodsNum = this.data.goodsNum;
    // var goodsTime = this.data.goodsTime;
    var goodsDes = this.data.goodsDes;
    var imgList = this.data.imgList;
    var imgStr = imgList.join(';');
    var goodsId = this.data.goodsId;
    var storeType = this.data.storeType;
    //上传还是修改
    var uploadType = this.data.uploadType;
    if (e.detail.value.goodsName == "") {
      wx.showToast({
        title: "请填写产品名称",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (e.detail.value.goodsValue == "") {
      wx.showToast({
        title: "请填写产品价值",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (e.detail.value.goodsNum == "") {
      wx.showToast({
        title: "请填写产品件数",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (uploadType == "add") {
      //上传 
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=AgentLoadGoodsStoreInfo"),
        method: 'post',
        data: baseJs.json2Form({
          UID: uid,
          GoodsName: e.detail.value.goodsName,
          Price: e.detail.value.goodsValue,
          GoodsNum: e.detail.value.goodsNum,
          expireTime: e.detail.value.goodsTime,
          Describe: goodsDes,
          GoodsType: storeType,
          ImgUrl: imgStr,
        }),
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          console.log(res.data);
          if (res.data.ResultFlag == 1) {
            wx.showToast({
              title: res.data.ResultMsg,
              icon: 'none',
              duration: 2000
            });
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          } else {
            wx.showToast({
              title: res.data.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }
        }
      })
    } else {
      // 修改
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=AgentUpdateGoodsStoreInfo"),
        method: 'post',
        data: baseJs.json2Form({
          GoodsName: e.detail.value.goodsName,
          Price: e.detail.value.goodsValue,
          GoodsNum: e.detail.value.goodsNum,
          expireTime: e.detail.value.goodsTime,
          Describe: goodsDes,
          GoodsId: goodsId,
          ImgUrl: imgStr,
        }),
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          console.log(res.data);
          if (res.data.ResultFlag == 1) {
            wx.showToast({
              title: res.data.ResultMsg,
              icon: 'none',
              duration: 2000
            });
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          } else {
            wx.showToast({
              title: res.data.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }
        }
      })
    }
  },
})